package com.osce.ex.controller;

import com.csce.domain.entity.exam.ExExercise;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.interfaces.ex.IExExerciseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 理论练习Controller
 *
 * @author zzx
 * @since 2023-08-02
 */
@RestController
@RequestMapping("/ex/exercise")
public class ExExerciseController extends BaseController {

    @Resource
    private IExExerciseService exExerciseService;

    /**
     * 查询理论练习列表
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExExercise exExercise) {
        exExercise.setUserId(getUserId());
        startPage();
        List<ExExercise> list = exExerciseService.selectExExerciseList(exExercise);
        return getDataTable(list);
    }

    /**
     * 导出理论练习列表
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:export')")
    @Log(title = "理论练习", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExExercise exExercise) {
        exExercise.setUserId(getUserId());
        List<ExExercise> list = exExerciseService.selectExExerciseList(exExercise);
        ExcelUtil<ExExercise> util = new ExcelUtil<ExExercise>(ExExercise.class);
        util.exportExcel(response, list, "理论练习数据");
    }

    /**
     * 获取理论练习详细信息
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(exExerciseService.selectExExerciseById(id));
    }

    /**
     * 新增理论练习
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:add')")
    @Log(title = "理论练习", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExExercise exExercise) {
        exExercise.setUserId(getUserId());
        return toAjax(exExerciseService.insertExExercise(exExercise));
    }

    /**
     * 创建理论练习
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:add')")
    @Log(title = "理论练习-创建", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult createExExercise(@Validated @RequestBody ExExercise exExercise) {
        exExercise.setUserId(getUserId());
        return toAjax(exExerciseService.createExExercise(exExercise)).data(exExercise);
    }

    /**
     * 修改理论练习
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:edit')")
    @Log(title = "理论练习", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExExercise exExercise) {
        exExercise.setUserId(getUserId());
        return toAjax(exExerciseService.updateExExercise(exExercise));
    }

    /**
     * 交卷
     *
     * @param id      练习id
     * @param timer   做题用时
     * @param answers 答案集合
     */
    @Log(title = "理论练习-交卷", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{id}/{timer}")
    public AjaxResult submit(@PathVariable("id") Long id, @PathVariable("timer") Integer timer, @RequestBody Map<Long, Object> answers) {
        return toAjax(exExerciseService.submit(id, timer, answers));
    }

    /**
     * 删除理论练习
     */
    @PreAuthorize("@ss.hasPermi('ex:exercise:remove')")
    @Log(title = "理论练习", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(exExerciseService.deleteExExerciseByIds(ids));
    }
}
